import requests
import json

def post_temp_hum(server, oid, temp, hum):  # 已测试
    data = {
        "oid": oid,
        "temp": temp,
        "hum": hum
    }
    requests.post(server + '/state', json=data)


def post_point(server, oid, point):  # 已测试
    data = {
        "oid": oid,
        "pt": point
    }
    response = requests.post(server + '/position', json=data)
    try:
        res_dic = json.loads(response.text)
        # print(res_dic)
        if res_dic['code'] == 20040:
            return False
        return True
    except:
        return False


def post_warning(server, car_id, order_id, warning_flag):  # 修改配送车状态(已测试)
    data = {
        "cid": car_id,
        "oid": order_id,
        "w": warning_flag
    }
    requests.post(server + '/warning', json=data)


def post_order_state(server, oid, state):  # 修改订单状态(已测试)
    data = {
        "oid": oid,
        "state": state
    }
    response = requests.post(server + '/order/state', json=data)
    res_dic = json.loads(response.text)
    if res_dic['code'] == 0:
        return True
    return False